<template>
	<view class="container">
		<view class="header">
			<view class="video-box">
				<video id="myVideo"
					src="https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/%E7%AC%AC1%E8%AE%B2%EF%BC%88uni-app%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D%EF%BC%89-%20DCloud%E5%AE%98%E6%96%B9%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B@20200317.mp4"
					danmu-btn controls></video>
			</view>
			
			<view class="video-info">
				<view class="title">
					<text>《嵌入式系统设计师真题讲解》</text>
				</view>
				<view class="extend">
					<text class="time">2022-03-09 22:08</text>
					<text class="time">时长：30分钟</text>
				</view>
			</view>
		</view>
		
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="chats-content">
			<view class="chat-item" :class="item.self?'self':''" v-for="(item, index) in messages" :key="index">
				<view class="avatar">
					<image :src="item.avatar" mode="aspectFit"></image>
				</view>
				<view class="message">
					<text>{{item.content}}</text>
				</view>
			</view>
		</scroll-view>

		<view class="send-content">
			<view class="send">
				<input v-model="content" type="text" placeholder="请输入评论弹幕">
				<view class="send-btn" @click="onSend">
					<text>发送</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scrollTop: 0,
				messages: [{
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!good',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!good',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!good',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!',
					self: false,
				}, {
					avatar: '/static/icons/kaodiandagang.png',
					content: '真不错!123',
					self: true,
				}],
				content: ''
			}
		},
		onLoad() {
			this.$nextTick(function() {
				this.scrollToBottom();
			})
		},
		methods: {
			scrollToBottom() {
				const that = this
				let view = uni.createSelectorQuery().in(that).select(".chats-content");
				view.fields({
					size: true,
					scrollOffset: true
				}, data => {
					that.scrollTop = data.scrollHeight;
					console.log(that.scrollTop)
				}).exec();
			},
			onSend() {
				if (this.content == '') return;
				const that = this;
				this.messages.push({
					avatar: '/static/icons/kaodiandagang.png',
					content: this.content,
					self: true,
				})
				this.content = ''
				uni.showToast({
					title: '发送成功'
				})
				this.$nextTick(function() {
					this.scrollToBottom();
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>
<style lang="scss" scoped>
	.container{
		padding-top: 560rpx;
	}
	.header{
		z-index: 99;
		position: fixed;
		top: 0;
	}
	
	.video-box {
		width: 750rpx;
		height: 400rpx;
		background-color: #fff;

		#myVideo {
			width: 750rpx;
			height: 400rpx;
		}
	}

	.send-content {
		position: fixed;
		bottom: 0;
		width: 750rpx;
		background: #fff;
		padding: 22rpx;
		box-sizing: border-box;
		z-index: 999;

		.send {
			width: 100%;
			display: flex;

			input {
				height: 88rpx;
				line-height: 88rpx;
				flex: 1;
			}

			.send-btn {
				background-color: #53a8ff;
				margin-left: 22rpx;
				border-radius: 88rpx;
				width: 160rpx;
				color: #fff;
				text-align: center;
				line-height: 88rpx;
			}
		}
	}

	.video-info {
		background-color: #fff;
		padding: 22rpx;

		.extend {
			margin-top: 12rpx;
			font-size: 26rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #888;
		}
	}

	.chats-content {
		padding: 22rpx;
		padding-bottom: 144rpx;
		box-sizing: border-box;

		.self {
			display: flex;
			justify-content: flex-end;
			align-items: flex-end;

			.avatar {
				order: 2 !important;
			}

			.message {
				order: 1 !important;
				background-color: #66b1ff !important;
				color: #fff !important;
				border-radius: 30rpx 30rpx 0 30rpx !important;
			}
		}

		.chat-item {
			display: flex;
			margin-bottom: 22rpx;

			.avatar {
				height: 90rpx;
				width: 90rpx;
				overflow: hidden;
				border-radius: 90rpx;
				background-color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					height: 65rpx;
					width: 65rpx;
				}
			}

			.message {
				margin-left: 22rpx;
				padding: 22rpx;
				background-color: #fff;
				border-radius: 30rpx 30rpx 30rpx 0;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				color: #333333;
			}
		}
	}
</style>
